package com.hema.auction.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.hema.auction.R;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.utils.Constant;
import com.hema.auction.utils.Utils;
import com.hema.auction.widget.view.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 描述:内置webview
 * 作者:john
 */
public class H5Activity extends BaseActivity {

    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R.id.webview)
    WebView webview;

    private String title;
    private String url;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_h5);
        ButterKnife.bind(this);
        titleView.setVisibility(View.VISIBLE);
        webview.setVisibility(View.VISIBLE);
        title = getIntent().getStringExtra(Constant.EXTRA_TITLE);
        url = getIntent().getStringExtra(Constant.EXTRA_URL);
        init();
    }

    @SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
    private void init() {
        if (Utils.isEmpty(title))
            titleView.setVisibility(View.GONE);
        titleView.setTitle(title);
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        webview.setWebChromeClient(new WebChromeClient() {//监听网页加载
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    progressBar.setVisibility(View.GONE);
                } else {
                    // 加载中
                    progressBar.setProgress(newProgress);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        webview.addJavascriptInterface(new JS(), "goodInfo");
        webview.loadUrl(url);
//        webview.loadUrl(UrlManager.H5_NEW_GUIDE);
    }

    public class JS {
        @JavascriptInterface
        public void detail(String id) {
            getDefaultHandler().post(new Runnable() {
                @Override
                public void run() {
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.EXTRA_GOOD_ID, id);
                    intentTo(GoodsDetailsActivity.class, bundle);
                    finish();
                }
            });
        }
    }
}
